//
//  TheNewChoseNextCollectionViewCell.m
//  NewMusic
//
//  Created by dllo on 15/11/17.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "TheNewChoseNextCollectionViewCell.h"
#import "ManyListenListTableViewCell.h"
#import "CustomerNetworking.h"
#import "HotListenListModel.h"
#import "TheNewChoseHeaderView.h"
@interface TheNewChoseNextCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>
@property(nonatomic, retain) HotListenListModel *hotListenListModel;
@property(nonatomic, assign) CGFloat contenfOffSetY;
@property(nonatomic, assign) CGFloat indexCellY;
@end


@implementation TheNewChoseNextCollectionViewCell
- (void)dealloc {
    [_songlistIdString release];
    [_buttonArray release];
    [_titleString release];
    [_index release];
    [_headerView release];
    [_hotListenListModel release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.indexCellY = 0;
        //创建视图
        [self createSubView];
    }
    return self;
}
#pragma mark 重写songlistIdString的setter方法
- (void)setSonglistIdString:(NSString *)songlistIdString {
    if ( _songlistIdString != songlistIdString) {
        [_songlistIdString release];
        _songlistIdString = [songlistIdString retain];
    }
    if (![songlistIdString isEqualToString:@""]) {
        [self loadData];
    }
}
#pragma mark 重写indexCellY的setter方法
- (void)setIndexCellY:(CGFloat)indexCellY {
    if (_indexCellY != indexCellY) {
        _indexCellY = indexCellY;
    }
    self.tableView.contentOffset = CGPointMake(0, indexCellY);
}
//获取数据
- (void)loadData {
    [CustomerNetworking customerConnectWithURL:[NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@?app=ttpod&v=v8.1.1.2015110616", _songlistIdString] andParameters:nil andBlock:^(id response) {
        self.hotListenListModel = [[HotListenListModel alloc]initWithDictionary:response];
        [_tableView reloadData];
    }];
}
#pragma mark 创建视图
- (void)createSubView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.contenfOffSetY = _tableView.contentOffset.y;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellAccessoryNone;
    //头视图
    self.headerView = [[TheNewChoseHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 250)];
    _tableView.tableHeaderView = _headerView;
    //注册cell的重用标识
    [_tableView registerClass:[ManyListenListTableViewCell class] forCellReuseIdentifier:@"ManyListenListTableViewCellIdentify"];
    [self.contentView addSubview:_tableView];
    [_tableView release];
    
}
#pragma mark cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ManyListenListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ManyListenListTableViewCellIdentify" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_hotListenListModel.songs.count != 0) {
        cell.dataSongsModel = [_hotListenListModel.songs objectAtIndex:indexPath.row];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.queueString = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return cell;
}
#pragma mark cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_hotListenListModel.songs.count != 0) {
        return _hotListenListModel.songs.count;
    }else {
        return 0;
    }
}
#pragma mark cell的大小
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.frame.size.height / 8;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate tableViewCellClickAction:_hotListenListModel.songs index:indexPath.row];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView) {
        if (scrollView.contentOffset.y < _contenfOffSetY ) {
            [self.delegate tableViewScrollFrame:scrollView.contentOffset.y - _contenfOffSetY tableViewContentOffY:scrollView.contentOffset.y top:NO];
        }else if (scrollView.contentOffset.y > _contenfOffSetY ) {
            [self.delegate tableViewScrollFrame:scrollView.contentOffset.y - _contenfOffSetY tableViewContentOffY:scrollView.contentOffset.y top:YES];
        }
        _contenfOffSetY = scrollView.contentOffset.y;
    }
    if (scrollView.contentOffset.y  < 0) {
        scrollView.contentOffset = CGPointMake(0, 0);
    }
}

@end
